/**
 * 文章模型
 * @module models/Article
 */

const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database');

/**
 * 文章模型定义
 * @typedef {Object} Article
 * @property {string} title - 文章标题
 * @property {string} content - 文章内容(Markdown)
 * @property {string} excerpt - 文章摘要
 * @property {string} image - 封面图片URL
 * @property {number} categoryId - 分类ID
 * @property {string} tags - 标签(JSON字符串)
 * @property {Date} publishDate - 发布日期
 * @property {boolean} published - 是否发布
 * @property {number} views - 访问量
 */
const Article = sequelize.define('Article', {
  title: {
    type: DataTypes.STRING,
    allowNull: false,
    validate: {
      notEmpty: true,
      len: [2, 200]
    }
  },
  content: {
    type: DataTypes.TEXT,
    allowNull: false,
    validate: {
      notEmpty: true
    }
  },
  excerpt: {
    type: DataTypes.TEXT,
    allowNull: true
  },
  image: {
    type: DataTypes.STRING,
    allowNull: true
  },
  tags: {
    type: DataTypes.STRING,
    allowNull: true,
    get() {
      const rawValue = this.getDataValue('tags');
      return rawValue ? JSON.parse(rawValue) : [];
    },
    set(value) {
      this.setDataValue('tags', JSON.stringify(value || []));
    }
  },
  publishDate: {
    type: DataTypes.DATE,
    allowNull: true
  },
  published: {
    type: DataTypes.BOOLEAN,
    allowNull: false,
    defaultValue: false
  },
  views: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0
  }
});

module.exports = Article; 